package com.jesse.manager;

import com.jesse.custom.component.CustomDialog;
import com.jesse.dao.ButtonDao;
import com.jesse.main.R;
import com.jesse.util.ViewUtil;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

/**
 * @author jeissie 2014/5/27
 */
public class PopupManager {
	
	private static Context mContext;
	
	private static PopupManager popupManager = null;
	
	private int titleBackgroundColor;
	
	private PopupManager() {
		titleBackgroundColor = mContext.getResources().getColor(R.color.green);
	}
	
	public void setTitleBackgroundColor(int titleBackgroundColor) {
		this.titleBackgroundColor = titleBackgroundColor;
	}

	public static PopupManager getInstance(Context context) {
		mContext = context;
		if (popupManager == null) {
			popupManager = new PopupManager();
		}
		return popupManager;
	}
	
	public void release() {
		popupManager = null;
	}
	
	public CustomDialog.Builder popupWithButton(Context context, String title, String content, String buttonName, final ButtonDao button) {
		return popupWithTwoButton(context, title, content, true, buttonName, button, 
				false, ViewUtil.getContext().getResources().getString(R.string.cancel), null);
	}
	
	public CustomDialog.Builder popupWithTwoButton(Context context, String title, String contentText, String positiveButtonName, final ButtonDao positiveButtonDao,
			String negativeButtonName, final  ButtonDao negativeButtonDao) {
		return popupWithTwoButton(context, title, contentText, true, positiveButtonName, positiveButtonDao, true, negativeButtonName, negativeButtonDao);
	}
	
	public CustomDialog.Builder popupWithImageAndButton(Context context, int rId, String title, String positiveButtonName, final ButtonDao positiveButtonDao) {
		return createWithImageAndTwoButton(context, rId, title, true, positiveButtonName, positiveButtonDao, false, "", null);
	}
	
	public CustomDialog.Builder popupWithImageAndTwoButton(Context context, int rId, String title, String positiveButtonName, final ButtonDao positiveButtonDao, 
			String negativeButtonName, final  ButtonDao negativeButtonDao) {
		return createWithImageAndTwoButton(context, rId, title, true, positiveButtonName, positiveButtonDao, true, negativeButtonName, negativeButtonDao);
	}
	
	public CustomDialog.Builder popupWithCustomViewAndButton(Context context, View view, String title, String positiveButtonName, final ButtonDao positiveButtonDao, 
			String negativeButtonName, final  ButtonDao negativeButtonDao) {
		return createWithCustomViewAndButton(context, view, title, true, positiveButtonName, positiveButtonDao, true, negativeButtonName, negativeButtonDao);
	}
	
	
	private CustomDialog.Builder createWithImageAndTwoButton(Context context, int rId, String title, Boolean positiveButton , String positiveButtonName, final ButtonDao positiveButtonDao, 
			Boolean negativeButton ,String negativeButtonName, final  ButtonDao negativeButtonDao) {
		ImageView imageView = new ImageView(context);
		imageView.setImageResource(rId);
		CustomDialog.Builder builder = createWithCustomViewAndButton(context, imageView, title, positiveButton, positiveButtonName, positiveButtonDao, negativeButton, negativeButtonName, negativeButtonDao);
		return builder;
	}

	private CustomDialog.Builder createWithCustomViewAndButton(Context context, View view, String title, Boolean positiveButton , String positiveButtonName, final ButtonDao positiveButtonDao, 
			Boolean negativeButton ,String negativeButtonName, final  ButtonDao negativeButtonDao) {
		CustomDialog.Builder builder = new CustomDialog.Builder(context);
		builder.setContentView(view);
		builder.setTitle(title);
		builder.setTitleBackgroundColor(titleBackgroundColor);
		if (positiveButton) {
			builder.setPositiveButton(positiveButtonName, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (positiveButtonDao != null) {
						positiveButtonDao.onClick();
	            	}
					ViewUtil.closePopup();
				}
			});
		}
		if (negativeButton) {
			builder.setNegativeButton(negativeButtonName, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (negativeButtonDao != null) {
						negativeButtonDao.onClick();
	            	}
					ViewUtil.closePopup();
				}
			});
		}
		return builder;
	}
	
	private CustomDialog.Builder  popupWithTwoButton(Context context, String title, String contentText, Boolean positiveButton, String positiveButtonName, final ButtonDao positiveButtonDao,
			Boolean negativeButton, String negativeButtonName, final  ButtonDao negativeButtonDao) {
		CustomDialog.Builder builder = new CustomDialog.Builder(context);
		builder.setMessage(contentText);
		builder.setTitle(title);
		builder.setTitleBackgroundColor(titleBackgroundColor);
		if (positiveButton) {
			builder.setPositiveButton(positiveButtonName, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (positiveButtonDao != null) {
						positiveButtonDao.onClick();
	            	}
					ViewUtil.closePopup();
				}
			});
		}
		if (negativeButton) {
			builder.setNegativeButton(negativeButtonName, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (negativeButtonDao != null) {
						negativeButtonDao.onClick();
	            	}
					ViewUtil.closePopup();
				}
			});
		}
		return builder;
	}
	
	public void autoClose(float time) {
		Handler x = new Handler();
		x.postDelayed(new Runnable() {
			public void run() {
				ViewUtil.closePopup();
			}
		}, (long) (time * 1000));
	}
}
